package com.bosssoft.trainee.nontax.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bosssoft.trainee.nontax.dao.UserMapper;
import com.bosssoft.trainee.nontax.entity.dto.PermissionDTO;
import com.bosssoft.trainee.nontax.entity.po.PermissionPO;
import com.bosssoft.trainee.nontax.dao.PermissionMapper;
import com.bosssoft.trainee.nontax.entity.po.UserPO;
import com.bosssoft.trainee.nontax.service.PermissionService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bosssoft.trainee.nontax.utils.MenuTree;
import com.bosssoft.trainee.nontax.entity.vo.RolePermissionVO;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.*;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author sct
 * @since 2022-06-15
 */
@Service
@Transactional
public class PermissionServiceImpl extends ServiceImpl<PermissionMapper, PermissionPO> implements PermissionService {

    @Resource
    private UserMapper userMapper;

    /**
     * 根据用户ID查询权限列表
     * @param userId
     * @return
     */
    @Override
    public List<PermissionPO> findPermissionListByUserId(Long userId) {
        return baseMapper.findPermissionListByUserId(userId);
    }

    /**
     * 查询菜单列表
     * @return
     */
    @Override
    public List<PermissionDTO> findPermissionList(PermissionDTO permissionDTO) {
        QueryWrapper<PermissionPO> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByAsc("order_num");
        List<PermissionPO> permissionPOList = baseMapper.selectList(queryWrapper);
        permissionPOList=MenuTree.makeMenuTree(permissionPOList, 0L);
        List<PermissionDTO> permissionDTOList = new ArrayList<>();
        BeanUtils.copyProperties(permissionPOList,permissionDTOList);
        return permissionDTOList;
    }

    /**
     * 查询父级列表
     * @return
     */
    @Override
    public List<PermissionDTO> findParentPermissionList() {
        QueryWrapper<PermissionPO> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("type", Arrays.asList(0,1));
        queryWrapper.orderByAsc("order_num");
        List<PermissionPO> permissionPOList = baseMapper.selectList(queryWrapper);
        PermissionPO permissionPO = new PermissionPO();
        permissionPO.setId(0L);
        permissionPO.setParentId(-1L);
        permissionPO.setLabel("顶级菜单");
        //将顶级菜单添加到集合
        permissionPOList.add(permissionPO);
        //生成菜单数据
        permissionPOList=MenuTree.makeMenuTree(permissionPOList, -1L);
        List<PermissionDTO> permissionDTOList = new ArrayList<>();
        BeanUtils.copyProperties(permissionPOList,permissionDTOList);
        return permissionDTOList;
    }

    /**
     * 是否有子菜单
     * @param id
     * @return
     */
    @Override
    public boolean hasChildrenOfPermission(Long id) {
        QueryWrapper<PermissionPO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("parent_id", id);
        //判断数量是否大于0，如果大于0则表示存在
        return baseMapper.selectCount(queryWrapper) > 0;
    }

    /**
     * 查询分配权限树列表
     * @param userId
     * @param roleId
     * @return
     */
    @Override
    public RolePermissionVO findPermissionTree(Long userId, Long roleId) {
        UserPO userPO = userMapper.selectById(userId);
        List<PermissionPO> list = null;
        // 判断当前用户角色，如果是管理员，则查询所有权限；如果不是管理员，则只查询自己所拥有的的权限
        if(!ObjectUtils.isEmpty(userPO.getIsAdmin())&& userPO.getIsAdmin() == 1){
            list=baseMapper.selectList(null);
        }else {
            list =baseMapper.findPermissionListByUserId(userId);
        }
        List<PermissionPO> permissionPOList = MenuTree.makeMenuTree(list, 0L);
        List<PermissionPO> rolePermissionPOS = baseMapper.findPermissionListByRoleId(roleId);
        List<Long> listIds = new ArrayList<>();
        Optional.ofNullable(list).orElse(new ArrayList<>())
                .stream()
                .filter(Objects::nonNull)
                .forEach(item -> Optional.ofNullable(rolePermissionPOS).orElse(new ArrayList<>())
                        .stream()
                        .filter(Objects::nonNull)
                        .forEach(obj ->{
                            if(item.getId().equals(obj.getId())){
                                listIds.add(obj.getId());
                                return;
                            }
                        }));
        RolePermissionVO vo = new RolePermissionVO();
        vo.setPermissionPOList(permissionPOList);
        vo.setCheckList(listIds.toArray());
        return vo;
    }
}
